//package com.hxkx.config;
//
//import cn.hutool.core.convert.Convert;
//import org.apache.http.Header;
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.client.CredentialsProvider;
//import org.apache.http.conn.ConnectionKeepAliveStrategy;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.http.impl.nio.reactor.IOReactorConfig;
//import org.apache.http.message.BasicHeader;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.List;
//
///**
// * @Author yunst
// * @data 2023/4/26 11:43
// */
//@Configuration
//public class ElasticsearchConfig {
//    @Autowired
//    private ElasticsearchProperties elasticsearchProperties;
//
//    @Bean
//    public RestHighLevelClient restHighLevelClient() {
//        RestClientBuilder restBuilder = RestClient
//                .builder(this.getHttpHosts());
//        Header[] defaultHeaders = new Header[]{
//                new BasicHeader("Content-Type", "application/json")
//        };
//        restBuilder.setDefaultHeaders(defaultHeaders);
//        restBuilder.setHttpClientConfigCallback(httpClientBuilder ->
//                httpClientBuilder
//                        .setKeepAliveStrategy(getConnectionKeepAliveStrategy())
//                        .setMaxConnPerRoute(10).
//                        setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()));
//        String username = elasticsearchProperties.getUsername();
//        String password = elasticsearchProperties.getPassword();
//        if (username != null && password != null) {
//            final CredentialsProvider credential = new BasicCredentialsProvider();
//            credential.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
//            restBuilder.setHttpClientConfigCallback(httpClientBuilder ->
//                    httpClientBuilder
//                            .setDefaultCredentialsProvider(credential)
//                            .setKeepAliveStrategy(getConnectionKeepAliveStrategy())
//                            .setMaxConnPerRoute(10)
//                            .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).build()));
//        }
//
//
//        restBuilder.setRequestConfigCallback(requestConfigBuilder ->
//                requestConfigBuilder.setConnectTimeout(1000)
//                        .setSocketTimeout(12 * 1000)
//                        .setConnectionRequestTimeout(-1));
//
//        RestHighLevelClient client = new RestHighLevelClient(restBuilder);
//        return client;
//    }
//
//
//    private ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() {
//        return (response, context) -> 2 * 60 * 1000;
//    }
//    private HttpHost[] getHttpHosts() {
//        List<String> clusterNodes = elasticsearchProperties.getUris();
//        HttpHost[] httpHosts = new HttpHost[clusterNodes.size()];
//        for (int i = 0; i < clusterNodes.size(); i++) {
//            String[] node = clusterNodes.get(i).split(":");
//            httpHosts[i] = new HttpHost(node[0], Convert.toInt(node[1]), "http");
//        }
//        return httpHosts;
//    }
//}
